package com.midian.common.system;

import java.util.HashMap;

import net.sf.json.JSONObject;

import com.google.gson.JsonObject;

public class RetManager {

	// 该项目私有结果码
	public static final String private_enroll_success = "enroll_success";
	public static final String private_enroll_fail = "enroll_fail";
	public static final String private_enroll_error = "enroll_error";
	public static final String private_activity_not_exist = "activity_not_exist";
	public static final String private_enroll_person_count_too_many = "enroll_person_count_too_many";
	public static final String private_report_success= "report_success";
	public static final String private_payway_no_support= "payway_no_support";
	
	public static final String private_no_repeat_enroll = "no_repeat_enroll";
	public static final String private_change_receive_status = "change_receive_status";
	/**
	 * 添加该项目私有的结果码
	 * 
	 * @param map
	 */
	private void addPrivateRet(HashMap<String, String> map) {
		map.put(private_enroll_success, "报名成功");
		map.put(private_enroll_fail, "报名失败");
		map.put(private_enroll_error, "报名错误");
		map.put(private_activity_not_exist, "活动不存在");
		map.put(private_enroll_person_count_too_many, "报名人数超出限制");
		map.put(private_report_success, "举报成功");
		map.put(private_payway_no_support, "支付方式不支付");
		map.put(private_no_repeat_enroll, "请勿重复报名");
		map.put(private_change_receive_status, "请修改推送状态接收最新消息");
	}

	// 公共结果码
	public static final String common_no_tip = "no_tip";
	public static final String common_success = "success";
	public static final String common_login_success = "login_success";
	public static final String common_register_success = "register_success";
	public static final String common_save_success = "save_success";
	public static final String common_submit_success = "submit_success";
	public static final String common_change_success = "change_success";
	public static final String common_update_success = "update_success";
	public static final String common_delete_success = "delete_success";
	public static final String common_validate_success = "validate_success";
	public static final String common_apply_success = "apply_success";
	public static final String common_comment_success = "comment_success";
	public static final String common_send_success = "send_success";
	public static final String common_fail = "fail";
	public static final String common_login_fail = "login_fail";
	public static final String common_register_fail = "register_fail";
	public static final String common_save_fail = "save_fail";
	public static final String common_submit_fail = "submit_fail";
	public static final String common_change_fail = "change_fail";
	public static final String common_update_fail = "update_fail";
	public static final String common_delete_fail = "delete_fail";
	public static final String common_validate_fail = "validate_fail";
	public static final String common_apply_fail = "apply_fail";
	public static final String common_comment_fail = "comment_fail";
	public static final String common_send_fail = "send_fail";
	public static final String common_error = "error";
	public static final String common_login_error = "login_error";
	public static final String common_register_error = "register_error";
	public static final String common_save_error = "save_error";
	public static final String common_submit_error = "submit_error";
	public static final String common_change_error = "change_error";
	public static final String common_update_error = "update_error";
	public static final String common_delete_error = "delete_error";
	public static final String common_validate_error = "validate_error";
	public static final String common_apply_error = "apply_error";
	public static final String common_comment_error = "comment_error";
	public static final String common_send_error = "send_error";
	public static final String common_token_error = "token_error";
	public static final String common_client_key_error = "client_key_error";
	public static final String common_client_key_expire = "client_key_expire";
	public static final String common_invalid_param = "invalid_param";
	public static final String common_account_not_exist = "account_not_exist";
	public static final String common_passwd_error = "passwd_error";
	public static final String common_account_or_passwd_error = "account_or_passwd_error";
	public static final String common_account_exist = "account_exist";
	public static final String common_verification_code_error = "verification_code_error";
	public static final String common_verification_code_expire = "verification_code_expire";
	public static final String common_verification_passed = "verification_passed";
	public static final String common_phone_not_exist = "phone_not_exist";
	public static final String common_phone_exist = "phone_exist";
	public static final String common_phone_registered = "phone_registered";
	public static final String common_user_disable = "user_disable";

	/**
	 * 添加公共结果码
	 * 
	 * @param map
	 */
	private void addCommonRet(HashMap<String, String> map) {
		map.put(common_no_tip, "该结果码表示不需要Toast提示，App端需作屏蔽处理");
		map.put(common_success, "成功");
		map.put(common_login_success, "登陆成功");
		map.put(common_register_success, "注册成功");
		map.put(common_save_success, "保存成功");
		map.put(common_submit_success, "提交成功");
		map.put(common_change_success, "修改成功");
		map.put(common_update_success, "更新成功");
		map.put(common_delete_success, "删除成功");
		map.put(common_validate_success, "验证成功");
		map.put(common_apply_success, "申请成功");
		map.put(common_comment_success, "评论成功");
		map.put(common_send_success, "发送成功");
		map.put(common_fail, "失败");
		map.put(common_login_fail, "登陆失败");
		map.put(common_register_fail, "注册失败");
		map.put(common_save_fail, "保存失败");
		map.put(common_submit_fail, "提交失败");
		map.put(common_change_fail, "修改失败");
		map.put(common_update_fail, "更新失败");
		map.put(common_delete_fail, "删除失败");
		map.put(common_validate_fail, "验证失败");
		map.put(common_apply_fail, "申请失败");
		map.put(common_comment_fail, "评论失败");
		map.put(common_send_fail, "发送失败");
		map.put(common_error, "系统错误");
		map.put(common_login_error, "登陆错误");
		map.put(common_register_error, "注册错误");
		map.put(common_save_error, "保存错误");
		map.put(common_submit_error, "提交错误");
		map.put(common_change_error, "修改错误");
		map.put(common_update_error, "更新错误");
		map.put(common_delete_error, "删除错误");
		map.put(common_validate_error, "验证错误");
		map.put(common_apply_error, "申请错误");
		map.put(common_comment_error, "评论错误");
		map.put(common_send_error, "发送错误");
		map.put(common_token_error, "token错误,需重新登陆获取新的token");
		map.put(common_client_key_error, "client_key错误");
		map.put(common_client_key_expire, "client_key过期");
		map.put(common_passwd_error, "密码错误");
		map.put(common_account_or_passwd_error, "帐号或密码错误");
		map.put(common_invalid_param, "参数为空/参数不全");
		map.put(common_account_not_exist, "不存在此账号");
		map.put(common_account_exist, "帐号已存在");
		map.put(common_verification_code_error, "验证码错误");
		map.put(common_verification_code_expire, "验证码失效");
		map.put(common_verification_passed, "验证通过");
		map.put(common_phone_not_exist, "手机号不存在");
		map.put(common_phone_exist, "手机号已存在");
		map.put(common_phone_registered, "该手机号已被注册");
		map.put(common_user_disable, "该帐号已被禁用");
	}

	private static RetManager instance;

	private static HashMap<String, String> map;
	
	
	public static final String RET = "ret";
	public static final String RET_CODE = "ret_code";
	public static final String RET_INFO = "ret_info";

	private RetManager() {
		map = new HashMap<>();
		addCommonRet(map);
		addPrivateRet(map);
	}

	public static RetManager getInstance() {
		if (instance == null) {
			instance = new RetManager();
		}
		return instance;
	}

	/**
	 * 根据结果码返回结果信息
	 * 
	 * @param ret_code
	 * @return
	 */
	public String getRetInfo(String ret_code) {
		if (!map.containsKey(ret_code)) {
			return "此结果码未定义";
		}
		return map.get(ret_code);
	}
	
	/**
	 * 新建一个包含ret ret_code ret_info的json对象
	 * @param ret ret的值
	 * @param retCode ret_code的值
	 * @return
	 */
	public JSONObject newRet(String ret,String retCode){
		JSONObject json = new JSONObject();
		json.put(RET, ret);
		json.put(RET_CODE, retCode);
		json.put(RET_INFO, getRetInfo(retCode));
		return json;
	}
	
	public JSONObject newRet(String ret,String retCode,String retInfo){
		JSONObject json = new JSONObject();
		json.put(RET, ret);
		json.put(RET_CODE, retCode);
		json.put(RET_INFO, retInfo);
		return json;
	}
	
	
	public JSONObject newRetSuccess(){
		JSONObject json = new JSONObject();
		json.put(RET, common_success);
		json.put(RET_CODE, common_success);
		json.put(RET_INFO, getRetInfo(common_success));
		return json;
	}
	
	
	public JSONObject newRetSuccess(String retCode){
		JSONObject json = new JSONObject();
		json.put(RET, common_success);
		json.put(RET_CODE, retCode);
		json.put(RET_INFO, getRetInfo(retCode));
		return json;
	}
	
	
	public JSONObject newRetSuccess(String retCode,String retInfo){
		JSONObject json = new JSONObject();
		json.put(RET, common_success);
		json.put(RET_CODE, retCode);
		json.put(RET_INFO, retInfo);
		return json;
	}
	
	
	/**
	 * 设置结果
	 * @param json 
	 * @param ret ret的值
	 * @param retCode ret_code的值
	 */
	public void setRet(JSONObject json,String ret,String retCode){
		json.put(RET, ret);
		json.put(RET_CODE, retCode);
		json.put(RET_INFO, getRetInfo(retCode));
	}
	
	/**
	 * 设置结果
	 * @param json 
	 * @param ret ret的值
	 * @param retCode ret_code的值
	 * @param retInfo ret_info的值
	 */
	public void setRet(JSONObject json,String ret,String retCode,String retInfo){
		json.put(RET, ret);
		json.put(RET_CODE, retCode);
		json.put(RET_INFO, retInfo);
	}
	
	
	public void setRetError(JSONObject json,String retCode){
		setRet(json, RetManager.common_error, retCode);
	}
	
	public void setRetError(JSONObject json,String retCode,String retInfo){
		setRet(json, RetManager.common_error, retCode,retInfo);
	}
	
	public void setRetSuccess(JSONObject json,String retCode){
		setRet(json, RetManager.common_success, retCode);
	}
	
	
	public void setRetSuccess(JSONObject json,String retCode,String retInfo){
		setRet(json, RetManager.common_success, retCode,retInfo);
	}
	
	
	public void setRetCode(JSONObject json,String retCode){
		json.put(RET_CODE, retCode);
		json.put(RET_INFO, getRetInfo(retCode));
	}
	
}
